﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;

namespace FinancialManagement.Controllers
{
    public class UploadController : Controller
    {
        public static string FilePath = "";

        public ActionResult Save(IEnumerable<HttpPostedFileBase> attachments)
        {            // The Name of the Upload component is "attachments"             
            foreach (var file in attachments)
            {                // Some browsers send file names with full path. This needs to be stripped.              
                var fileName = Path.GetFileName(file.FileName);
                var physicalPath = Path.Combine(Server.MapPath("~/Downloads"), fileName);
                // The files are not actually saved in this demo               
                file.SaveAs(physicalPath);
                FilePath = "../Downloads/" + fileName;
            }
            // Return an empty string to signify success        
            return Content("");
        }

        public ActionResult Remove(string[] fileNames)
        {
            // The parameter of the Remove action must be called "fileNames"           
            foreach (var fullName in fileNames)
            {
                var fileName = Path.GetFileName(fullName);
                var physicalPath = Path.Combine(Server.MapPath("~/Downloads"), fileName);
                // TODO: Verify user permissions             
                if (System.IO.File.Exists(physicalPath))
                {
                    // The files are not actually removed in this demo                  
                    System.IO.File.Delete(physicalPath);
                }
            }
            // Return an empty string to signify success            
            return Content("");
        }


    }
}
